# coding=utf-8
import codecs
import json

import pymongo

from manager.task import Task
from manager.taskJsonFileOutput import TaskJsonFileOutput

task_size = 1000
client = pymongo.MongoClient("mongodb://127.0.0.1:27017/pt-asia")
db = client.get_database()
node_id_cursor = db.browseTreeIncluded.find().limit(31166).skip(0)
node_id_array = []
task_id = 1
for result in node_id_cursor:
    browseNodeId = result["Node"]["browseNodeId"]
    node_id_array.append(browseNodeId)
    if len(node_id_array) >= task_size:
        t = Task(task_id, node_id_array)
        t.output(TaskJsonFileOutput('./tasks/task_%s.json' % t.task_id)).do()
        node_id_array = []
        task_id += 1
#
# with codecs.open("node_id_array.json", mode='ab+', encoding='utf-8') as f:
#     line = json.dumps(node_id_array)
#     f.write(line.decode("unicode_escape"))
#
# with codecs.open("node_id_array.json", mode='r', encoding='utf-8') as f:
#     r = json.load(f)
#     print(r)
